<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserLessonsTable extends Migration
{

    protected $tableName = 'user_lessons';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id')->comment('用户ID');
            $table->unsignedInteger('lesson_id')->comment('课时ID');
            $table->unsignedInteger('course_id')->comment('课程ID');
            $table->unsignedSmallInteger('sequence')->comment('排序');
            $table->unsignedSmallInteger('word_count')->default(0)->comment('单词数量');
            $table->unsignedSmallInteger('knowledge_count')->default(0)->comment('知识点数量');

            $table->unsignedSmallInteger('essence_qa_count')->default(0)->comment('精读问答数量');
            $table->unsignedSmallInteger('game_test_count')->default(0)->comment('游戏数量');
            $table->unsignedSmallInteger('game_word_count')->default(0)->comment('游戏单词数量');

            $table->string('progress')->nullable()->comment('学习进度');

            $table->tinyInteger('is_dub')->default(0)->comment('是否配音');
            $table->unsignedTinyInteger('dub_score')->default(0)->comment('配音评分');
            $table->unsignedTinyInteger('dub_star')->default(0)->comment('配音评星');
            $table->timestamp('last_dub_time')->nullable()->comment('最后配音时间');

            $table->unsignedSmallInteger('play_game_time')->comment('游戏闯关时间，单位s');
            $table->unsignedSmallInteger('game_hit_count')->default(0)->comment('游戏有效点击次数');
            $table->unsignedTinyInteger('game_score')->default(0)->comment('闯关得分');

            $table->unsignedSmallInteger('essence_hit_count')->default(0)->comment('精读有效点击次数');
            $table->unsignedTinyInteger('essence_score')->default(0)->comment('精读得分');

            $table->unsignedTinyInteger('score')->default(0)->comment('课程得分');
            $table->unsignedTinyInteger('star')->default(0)->comment('课程评星');

            $table->timestamp('enable_time')->nullable()->comment('启用时间，控制推送的展示');
            $table->timestamp('first_study_time')->nullable()->comment('初次学习时间');
            $table->timestamp('last_study_time')->nullable()->comment('最后学习时间，用来统计用户学习记录');

            $table->timestamps();
            $table->softDeletes();

            $table->index(['user_id', 'course_id'], 'user_course');
            $table->index(['user_id', 'lesson_id'], 'user_lesson');
        });
        DB::statement("ALTER TABLE `$this->tableName` comment '用户课程学习记录表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
